<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function __construct(){
        parent::__construct();
        $this->app_menu = M('app_menu');
        $this->menu = M('menu');
    }
    /*
    title : 后台首页
	time : 2018-05-03 14:48
	username : 安林
     */
    public function Index()
    {
    	$data = $this->menu->field('id,title,icon,ord')->where(array('level'=>1))->order('ord asc')->select();
    	foreach ($data as $key => $value) {
    		$data[$key]['list'] = $this->menu->field('*')->where(array('top_id'=>$value['id'],'display'=>1))->select();
    	}
    	$this->data = $data;
        $this->display();
    }
    /*
    title : 导航
	time : 2018-05-03 14:48
	username : 安林
     */
    public function Navigation()
    {
    	$data = $this->menu->order('ord desc')->select();
    	$this->data = $data;
        $this->display();
    }

    /*
    title : 搜索框接口
	time : 2018-05-04 14:48
	username : 安林
     */
    public function Suos()
    {
    	$s = I('get.p');
    	$callback = I('get.callback');
    	$where['name']  = array('like', '%'.$s.'%');
    	$list = M('coupon')->field('name,money')->where($where)->select();
    	foreach ($list as $key => $value) {
    		$list[$key] = array_values($value);
    	}
    	$data['result'] = $list;
    	$jsonstr = json_encode($data);
    	echo $callback.'('.$jsonstr.')';
    }
    /*
    title : 导航等级查询接口
	time : 2018-05-04 10:23
	username : 安林
     */
    public function Nav_level()
    {
    	$level = I('post.level');
    	$data = $this->menu->field('id,title')->where(array('level'=>$level-1))->order('id desc')->select();
    	$this->ajaxReturn($data,'JSON');
    }
    /*
    title : 添加导航
	time : 2018-05-04 11:13
	username : 安林
     */
    public function Navigation_add()
    {
    	if (IS_POST) {
	    	$data = I('post.arr');
	    	if($data['title']){
	    		$is_name = $this->menu->where(array('title'=>$data['title']))->find();
	    		if($is_name){
	    			$this->error('菜单名称已存在！不能重复',U('index/Navigation_add'));
	    		}else{
	            	$res = $this->menu->add($data);
	    		}
	        }else{
	            $this->error('请填写导航名称！',U('index/Navigation_add'));
	            exit;
	        }
	        if($res){
	            $this->success('添加成功！',U('index/Navigation'));
	            exit;
	        }else{
	            $this->error('添加失败！',U('index/Navigation_add'));
	            exit;
	        }
    	}
    	$this->display();
    }

    /*
    title : 编辑导航
	time : 2018-05-04 11:38
	username : 安林
     */
    public function Navigation_edit()
    {
    	$id = I('get.id');
    	$list = $this->menu->where(array('id'=>$id))->find();
        $level = $this->menu->field('id,title')->where(array('level'=>$list['level']-1))->select();
    	if (IS_POST) {
    		$data = I('post.arr');
    		if ($data['title']) {
    			$is_name = $this->menu->where(array('title'=>$data['title']))->where('id!=$id')->find();
    			if ($is_name) {
    				$this->error('菜单名称已存在！不能重复',U('index/Navigation_edit'));
    				exit;
    			}else{
    				$res = $this->menu->where(array('id'=>$id))->save($data);
    			}
    		}else{
    			 $this->error('请填写导航名称！',U('index/Navigation_edit'));
	            exit;
    		}
    		if ($res) {
    			$this->success('修改成功！',U('index/Navigation'));
	            exit;
    		}else{
    			$this->error('修改失败！',U('index/Navigation_edit?id='.$id));
	            exit;
    		}
    	}
        $this->list = $list;
    	$this->level = $level;
    	$this->display();
    }
    /*
    title : 导航删除
    time : 2018-05-16 15:51
    username : 安林
     */
    public function Navigation_del()
    {
        $id = I('post.id');
        $res = $this->menu->where(array('id'=>$id))->delete();
        if ($res) {
            $data = [
                'res'   =>  200,
                'msg'   =>  '删除成功!'
            ];
        }else {
            $data = [
                'res'   =>  400,
                'msg'   =>  '删除失败(错误代码401)!'
            ];
        }
        $this->ajaxReturn($data);
    }
    /*
    title : app导航
    time : 2018-05-16 16:32
    username : 安林
     */
    public function App_Navigation()
    {
        $data = $this->app_menu->select();
        //var_dump($data);
        $this->data = $data;
        $this->display();
    }
    /*
    title : app导航
    time : 2018-05-16 16:32
    username : 安林
     */
    public function App_Navigation_add()
    {
        if (IS_POST) {
            $data = I('post.arr');
            if($data['title']){
                $is_name = $this->app_menu->where(array('title'=>$data['title']))->find();
                if($is_name){
                    $this->error('菜单名称已存在！不能重复',U('index/App_Navigation_add'));
                }else{
                    $res = $this->app_menu->add($data);
                }
            }else{
                $this->error('请填写导航名称！',U('index/App_Navigation_add'));
                exit;
            }
            if($res){
                $this->success('添加成功！',U('index/App_Navigation'));
                exit;
            }else{
                $this->error('添加失败！',U('index/App_Navigation_add'));
                exit;
            }
        }
        $this->display();
    }
}